//
//  CTPersonalMessageCell.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/6/10.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTPersonalMessageCell.h"
#import "CTDebugManager.h"
#import "CTHeader.h"
#import "CTMessageItems.h"
#import "CTAppLanguage.h"
#import "CTRequestUtil.h"
#import "CTTextManager.h"

@interface CTPersonalMessageCell()
{
    UIImageView * headImageView;
    UIImageView * contenImageView;
    UIImageView * isNotReadImageView;
    
    UILabel * nameLabel;
    UILabel * describeLabel;
    UILabel * timeLabel;
    
    UIView * bottomLine;
    
    CTMessageListItem * currentMesgLitItem;
}

@end


@implementation CTPersonalMessageCell
@synthesize delegate;
- (void)dealloc
{
    currentMesgLitItem = nil;
    
    [bottomLine removeFromSuperview];
    bottomLine = nil;
    
    [headImageView removeFromSuperview];
    headImageView = nil;
    
    [contenImageView removeFromSuperview];
    contenImageView = nil;
    
    [nameLabel removeFromSuperview];
    nameLabel = nil;
    
    [describeLabel removeFromSuperview];
    describeLabel = nil;
    
    [timeLabel removeFromSuperview];
    timeLabel = nil;
    
    [isNotReadImageView removeFromSuperview];
    isNotReadImageView = nil;
    
    delegate = nil;
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTPersonalMessageCell Is Removed!!!"];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = CT_HEADER_WHITE_COLOR;
        
        [self creatSubViews];
    }
    return self;
}

-(void)creatSubViews
{
    headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 15, 45, 45)];
    headImageView.layer.cornerRadius = 22.5;
    headImageView.image = [UIImage imageNamed:@"head"];
    headImageView.layer.masksToBounds = YES;
    [self addSubview:headImageView];
    
    isNotReadImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 15, 10, 10)];
    isNotReadImageView.image = [UIImage imageNamed:@"weidutongzhi"];
    isNotReadImageView.hidden = YES;
    [self addSubview:isNotReadImageView];
    
    contenImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CT_HEADER_SCREEN_WIDTH - 10-50, 13, 50, 50)];
    contenImageView.userInteractionEnabled = YES;
    [self addSubview:contenImageView];
    
    UITapGestureRecognizer* singleRecognizer;
    singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom)];
    singleRecognizer.numberOfTapsRequired = 1; // 单击
    [contenImageView addGestureRecognizer:singleRecognizer];
    
    
    nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, 20, 150, 15)];
    nameLabel.font = [UIFont systemFontOfSize:15.0];
    nameLabel.textColor = CT_HEADER_DEEPLINE_COLOR;
    [self addSubview:nameLabel];
    
    describeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [describeLabel setNumberOfLines:0];
    describeLabel.lineBreakMode = NSLineBreakByWordWrapping;
    describeLabel.font = [UIFont systemFontOfSize:16.0];
    describeLabel.textColor = CT_HEADER_DEEPLINE_COLOR;
    [self addSubview:describeLabel];
    
    timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    timeLabel.font = [UIFont systemFontOfSize:12.0];
    timeLabel.textColor = [UIColor colorWithRed:203/255.0 green:203/255.0 blue:199/255.0 alpha:1.0];
    [self addSubview:timeLabel];
    
    
    bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 0.3)];
    bottomLine.backgroundColor = [UIColor colorWithRed:201/255.0 green:201/255.0 blue:201/255.0 alpha:1.0];
    [self addSubview:bottomLine];
}


#pragma mark - setter -

-(void)setPersonalMessageItem:(CTMessageListItem *)item
{
 
    currentMesgLitItem = item;
  
    [CTRequestUtil requestImgWithUrl:item.user.profile_image_url_thumb withCompletionSuccessHandler:^(UIImage *img) {
        headImageView.image = img;
    } withFailedHandler:^(NSError *error) {
        
    }];
    
   
    [CTRequestUtil requestImgWithUrl:item.thumb withCompletionSuccessHandler:^(UIImage *img) {
        contenImageView.image = img;
    } withFailedHandler:^(NSError *error) {
        
    }];
    
    
    nameLabel.text = item.user.name;
    
    
    
    NSString * typeStr;
    if ([item.type isEqualToString:@"1"]) {
        //点赞
        typeStr = [CTAppLanguage localizedStringForKey:@"message_agr" withValue:nil withTable:@"Language"];
        describeLabel.textColor = [UIColor blackColor];
        describeLabel.frame = CGRectMake(65,45,(CT_HEADER_SCREEN_WIDTH - 60-65-30), 16);
        describeLabel.text = typeStr;
        
        contenImageView.hidden = NO;
        
    }else if([item.type isEqualToString:@"2"]){
        //关注
        typeStr = [CTAppLanguage localizedStringForKey:@"message_follow" withValue:nil withTable:@"Language"];
        describeLabel.textColor = [UIColor blackColor];
        describeLabel.frame = CGRectMake(65,45,(CT_HEADER_SCREEN_WIDTH - 60-65-30), 16);
        describeLabel.text = typeStr;
        
        contenImageView.hidden = YES;
        
    }else{
        //评论
        typeStr = [CTAppLanguage localizedStringForKey:@"message_comment" withValue:nil withTable:@"Language"];
        NSString * commentStr = [typeStr stringByAppendingString:item.comment_text];
        
        NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:commentStr];
        [attriStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,5)];
        [attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16.0] range:NSMakeRange(0, 5)];
        
        CGSize labelSize;
        labelSize = [commentStr sizeWithFont:[UIFont systemFontOfSize:16.0] constrainedToSize:CGSizeMake((CT_HEADER_SCREEN_WIDTH - 60-65-15), 1000) lineBreakMode:NSLineBreakByWordWrapping];
        describeLabel.frame = CGRectMake(65,45,labelSize.width, labelSize.height);
        describeLabel.attributedText = attriStr;
        
        contenImageView.hidden = NO;
    }
    
    
    timeLabel.frame = CGRectMake(65, describeLabel.frame.origin.y + describeLabel.bounds.size.height +12, 150, 12);
    timeLabel.text = [[CTTextManager shareInstance] timestampToTime:item.update_time];
    
    
    self.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, timeLabel.frame.origin.y + timeLabel.bounds.size.height +15);
    bottomLine.frame = CGRectMake(0,self.bounds.size.height - 0.5, CT_HEADER_SCREEN_WIDTH, 0.5);
    if ([item.isRead isEqualToString:@"0"])isNotReadImageView.hidden = NO;
    else isNotReadImageView.hidden = YES;
    
}


-(void)handleSingleTapFrom
{
    if ([self.delegate respondsToSelector:@selector(imageClicked:withItem:)]) {
        [self.delegate imageClicked:self withItem:currentMesgLitItem];
    }
}
@end
